package org.example.ftp.controller;

import org.example.ftp.entity.User;
import org.example.ftp.ftpservice.FtpUtil;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.UUID;

@RestController
@RequestMapping("/ftp")
public class FtpController {

    @PostMapping("/uploadftp")
    public Object uploadftp(User user, @RequestParam("file")MultipartFile file) throws Exception {
        // 获取上传的文件流
        InputStream inputStream = file.getInputStream();
        // 获取上传的文件名
        String filename = file.getOriginalFilename();
        // 截取后缀
        String suffix = filename.substring(filename.lastIndexOf("."));
        // 使用uuid拼接后缀，定义一个不重复的文件名
        String fileName = UUID.randomUUID()+suffix;
        // 调用自定义的FTP工具类上传文件
        boolean flag = FtpUtil.uploadFile(fileName, inputStream);
        return  "success";
    }
}
